const express = require("express");
const router = express.Router();
const db = require("../../utils/db");

// 药品调价渲染
router.post("/DrugPriceGet", (req, res) => {
  const { page, pageSize, m_type, m_name } = req.body;
  let sql = ``;
  if (m_type == "2") {
    sql = `SELECT * FROM medicinal_information WHERE m_name LIKE '%${m_name}%' LIMIT ${
      (page - 1) * pageSize
    },${pageSize}`;
  } else {
    sql = `SELECT * FROM medicinal_information WHERE m_type=${m_type} and m_name LIKE '%${m_name}%' LIMIT ${
      (page - 1) * pageSize
    },${pageSize}`;
  }
  let sql1 = `SELECT COUNT(*) AS total FROM medicinal_information`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      db.query(sql1, (err2, data2) => {
        if (err2) {
          res.send({ error: 1, msg: err2 });
        } else {
          res.send({ error: 0, data: data, total: data2[0].total });
        }
      });
    }
  });
});
// 调价记录渲染
router.post("/PriceRecordGet", (req, res) => {
  const { page, pageSize, m_type, m_name } = req.body;
  let sql = ``;
  if (m_type == "2") {
    sql = `SELECT * FROM medicinal_information WHERE m_name LIKE '%${m_name}%' LIMIT ${
      (page - 1) * pageSize
    },${pageSize}`;
  } else {
    sql = `SELECT * FROM medicinal_information WHERE m_type=${m_type} and m_name LIKE '%${m_name}%' LIMIT ${
      (page - 1) * pageSize
    },${pageSize}`;
  }
  let sql1 = `SELECT COUNT(*) AS total FROM medicinal_information`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      db.query(sql1, (err2, data2) => {
        if (err2) {
          res.send({ error: 1, msg: err2 });
        } else {
          res.send({ error: 0, data: data, total: data2[0].total });
        }
      });
    }
  });
});

// 新增调价渲染

router.post("/addPricingGet", (req, res) => {
  const { id } = req.body;
  let sql = `SELECT *,COUNT(d_m_id) AS total FROM medicinal_information  LEFT JOIN detail_price ON detail_price.d_m_id=medicinal_information.m_id WHERE  m_id='${id}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});
// 新增调价
router.post("/addPricing", (req, res) => {
  const { m_id, u_id, d_price, date } = req.body;
  const sql = ` INSERT INTO detail_price VALUES(NULL,'${m_id}','${u_id}','${d_price}','${date}');`;
  const sql1 = `UPDATE medicinal_information SET m_price='${d_price}' WHERE m_id='${m_id}';`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      db.query(sql1, (err2, data2) => {
        if (err2) {
          res.send({ error: 1, msg: err2 });
        } else {
          res.send({ error: 0, msg: "调价成功" });
        }
      });
    }
  });
});
// 查看调价详情
router.post("/checkRecordGet", (req, res) => {
  const { id } = req.body;
  let sql = `SELECT * FROM medicinal_information  LEFT JOIN detail_price ON detail_price.d_m_id=medicinal_information.m_id LEFT JOIN doctor ON detail_price.d_u_id=doctor.u_id WHERE d_m_id=${id}`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, data: data });
    }
  });
});

// 药品信息维护数据渲染
router.post("/infoData", (req, res) => {
  const { page, pageSize, m_type, n_status, m_name } = req.body;
  let sql = ``;
  if (m_type == "2") {
    sql = `SELECT * FROM medicinal_information WHERE n_status LIKE'%${n_status}%' AND m_name LIKE '%${m_name}%'
    LIMIT ${(page - 1) * pageSize},${pageSize}`;
  } else {
    sql = `SELECT * FROM medicinal_information WHERE m_type=${m_type} and n_status LIKE '%${n_status}%' AND m_name LIKE '%${m_name}%'
    LIMIT ${(page - 1) * pageSize},${pageSize}`;
  }
  let sql1 = `SELECT COUNT(*) AS total FROM medicinal_information`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ err, msg: "error" });
    } else {
      db.query(sql1, (err2, data2) => {
        if (err2) {
          res.send({ err2: err, msg: "error" });
        } else {
          res.send({
            error: 0,
            msg: "success",
            data: data,
            total: data2[0].total,
          });
        }
      });
    }
  });
});
// 编辑附
router.post("/edit", (req, res) => {
  const { m_company, m_name, m_price, n_status, m_spec, m_type, m_drug, m_id } =
    req.body;
  let sql = `UPDATE medicinal_information SET m_company='${m_company}', m_name = '${m_name}',
    m_price = ${m_price},n_status = '${n_status}',m_spec='${m_spec}',m_type='${m_type}',m_drug=${m_drug} WHERE m_id=${m_id};`;
    db.query(sql, (err, data) => {
    if (err) {
      res.send({ err, msg: "error" });
    } else {
      res.send({ data: data, msg: "success" });
    }
  });
});
module.exports = router;
